#ifndef K_GUARD_H
#define K_GUARD_H
#pragma once

#include "KNoCopy.h"
#include <functional>
#include <utility>

class KGuard : KNoCopy {
public:
    typedef std::function<void ()> GuardType;
public:
    explicit KGuard(GuardType &&guard) : m_guard(std::move(guard)) {}
    explicit KGuard(const GuardType &guard) : m_guard(guard) {}
    ~KGuard();
public:
    void cancel();
private:
    GuardType m_guard;
};

inline KGuard::~KGuard()
{
    if (m_guard) {
        m_guard();
    }
}

inline void KGuard::cancel()
{
    m_guard = GuardType();
}

#endif // K_GUARD_H